package org.jeecg.modules.demo.hd.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.HttpServletRequestUtil;
import org.jeecg.common.vo.RequestVo;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.demo.hd.entity.AppUserShareMain;
import org.jeecg.modules.demo.hd.service.IAppUserShareMainService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.hd.vo.ShareTrendVo;
import org.jeecg.modules.system.entity.AppUserDeviceLog;
import org.jeecg.modules.system.service.IAppUserDeviceLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: app_user_share_main
 * @Author: jeecg-boot
 * @Date: 2025-04-30
 * @Version: V1.0
 */
@Api(tags = "app_user_share_main")
@RestController
@RequestMapping("/hd/appUserShareMain")
@Slf4j
public class AppUserShareMainController extends JeecgController<AppUserShareMain, IAppUserShareMainService> {
    @Autowired
    private IAppUserShareMainService appUserShareMainService;
    @Autowired
    private IAppUserDeviceLogService deviceLogService;

    /**
     * 分页列表查询
     *
     * @param appUserShareMain
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "app_user_share_main-分页列表查询")
    @ApiOperation(value = "app_user_share_main-分页列表查询", notes = "app_user_share_main-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AppUserShareMain>> queryPageList(AppUserShareMain appUserShareMain,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        LambdaQueryWrapper<AppUserShareMain> queryWrapper = Wrappers.lambdaQuery();
//        QueryWrapper<AppUserShareMain> queryWrapper = QueryGenerator.initQueryWrapper(appUserShareMain, req.getParameterMap());
        Page<AppUserShareMain> page = new Page<AppUserShareMain>(pageNo, pageSize);
        IPage<AppUserShareMain> pageList = appUserShareMainService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param appUserShareMain
     * @return
     */
    @AutoLog(value = "app_user_share_main-添加")
    @ApiOperation(value = "app_user_share_main-添加", notes = "app_user_share_main-添加")
    @RequiresPermissions("hd:app_user_share_main:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AppUserShareMain appUserShareMain) {
        appUserShareMainService.save(appUserShareMain);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param appUserShareMain
     * @return
     */
    @AutoLog(value = "app_user_share_main-编辑")
    @ApiOperation(value = "app_user_share_main-编辑", notes = "app_user_share_main-编辑")
    @RequiresPermissions("hd:app_user_share_main:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody AppUserShareMain appUserShareMain) {
        appUserShareMainService.updateById(appUserShareMain);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "app_user_share_main-通过id删除")
    @ApiOperation(value = "app_user_share_main-通过id删除", notes = "app_user_share_main-通过id删除")
    @RequiresPermissions("hd:app_user_share_main:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        appUserShareMainService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "app_user_share_main-批量删除")
    @ApiOperation(value = "app_user_share_main-批量删除", notes = "app_user_share_main-批量删除")
    @RequiresPermissions("hd:app_user_share_main:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.appUserShareMainService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "app_user_share_main-通过id查询")
    @ApiOperation(value = "app_user_share_main-通过id查询", notes = "app_user_share_main-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AppUserShareMain> queryById(@RequestParam(name = "id", required = true) String id) {
        AppUserShareMain appUserShareMain = appUserShareMainService.getById(id);
        if (appUserShareMain == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(appUserShareMain);
    }


    @ApiOperation(value = "工币趋势数据", notes = "工币趋势数据")
    @GetMapping(value = "/getShareTrend")
    @IgnoreAuth
    public Result<List<ShareTrendVo>> getShareTrend() {
        List<ShareTrendVo> list = new ArrayList<>();
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 生成前7天的日期
        for (int i = 6; i >= 0; i--) {
            LocalDate pastDate = today.minusDays(i);
            String formattedDate = pastDate.format(formatter);
            list.add(new ShareTrendVo(formattedDate, 1d));
        }
        return Result.OK(list);
    }


    @ApiOperation(value = "查询用户账户余额", notes = "查询用户账户余额")
    @GetMapping(value = "/queryCurrentShare")
    public Result<Double> queryCurrentShare(HttpServletRequest req) {
        LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = currentUser.getId();
        // 保存设备请求日志
        RequestVo requestVo = HttpServletRequestUtil.getDeviceInfoVo(req);
        AppUserDeviceLog deviceInfoVo = new AppUserDeviceLog();
        BeanUtils.copyProperties(requestVo, deviceInfoVo);
        deviceInfoVo.setUserId(currentUser.getId());
        deviceInfoVo.setRequestUrl("查询用户账户余额");
        deviceLogService.save(deviceInfoVo);
        Double aFloat = appUserShareMainService.queryCurrentShare(userId);
        return Result.OK(aFloat);
    }
}
